package com.gs.cd.service.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.Binder;
import com.gs.cd.service.auth.model.AuthUser;
import com.gs.cd.service.auth.mapper.AuthUserMapper;
import com.gs.cd.service.auth.model.AuthUserRole;
import com.gs.cd.service.auth.model.vo.AuthUserVo;
import com.gs.cd.service.auth.service.AuthUserRoleService;
import com.gs.cd.service.auth.service.AuthUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author seven
 * @since 2020-12-22
 */
@Service
public class AuthUserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> implements AuthUserService {

    @Override
    public AuthUser getByLoginName(String loginName) {
        return getOne(new QueryWrapper<AuthUser>().lambda().eq(AuthUser::getLoginName, loginName));
    }

    @Override
    public AuthUser getByEmail(String email) {
        return getOne(new QueryWrapper<AuthUser>().lambda().eq(AuthUser::getEmail, email));

    }

    @Autowired
    AuthUserRoleService authUserRoleService;

    @Override
    public AuthUserVo listWithRole(String loginName, String tenantCode) {
        List<AuthUserRole> list = authUserRoleService.listByUserLoginName(loginName, tenantCode);
        List<AuthUserRole> authUserRoles = Binder.convertAndBindRelations(list, AuthUserRole.class);
        AuthUser authUser = getByLoginName(loginName);
        AuthUserVo authUserVo = new AuthUserVo();
        BeanUtil.copyProperties(authUser, authUserVo);
        authUserVo.setAuthRoleList(authUserRoles.stream().map(AuthUserRole::getAuthRole).collect(Collectors.toList()));
        return authUserVo;
    }
}
